﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;

namespace EIA.SLClient.ProjectManage.Approve
{
    public class ProjectApproveViewModel : SimpleMvvmToolkit.ViewModelBase<ProjectApproveViewModel>
    {
        public ProjectApproveViewModel()
        {

        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected EIA.PM.Model.TProjectBasic _ProjectBasic;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.PM.Model.TProjectBasic ProjectBasic
        {
            get { return _ProjectBasic; }
            set
            {
                _ProjectBasic = value;
                NotifyPropertyChanged(m => m.ProjectBasic);
            }
        }

        protected EIA.PM.Model.LProjectApprove _Approve1;
        /// <summary>
        /// 一审
        /// </summary>
        public EIA.PM.Model.LProjectApprove Approve1
        {
            get { return _Approve1; }
            set
            {
                _Approve1 = value;
                NotifyPropertyChanged(m => m.Approve1);
                if (value != null)
                {
                    Approver1 = ListsResource.Instance.EngineerCollection.FirstOrDefault((o) => { return o.StfId == Approve1.Step1Staff; });
                }
                
            }
        }

        protected EIA.PM.Model.LProjectApprove _Approve2;
        /// <summary>
        /// 二审
        /// </summary>
        public EIA.PM.Model.LProjectApprove Approve2
        {
            get { return _Approve2; }
            set
            {
                _Approve2 = value;
                NotifyPropertyChanged(m => m.Approve2);
                if (value != null) Approver2 = ListsResource.Instance.ApproverCollection.FirstOrDefault((o) => { return o.StfId == Approve2.Step1Staff; });
            }
        }

        protected EIA.PM.Model.LProjectApprove _Approve3;
        /// <summary>
        /// 三审
        /// </summary>
        public EIA.PM.Model.LProjectApprove Approve3
        {
            get { return _Approve3; }
            set
            {
                _Approve3 = value;
                NotifyPropertyChanged(m => m.Approve3);
                if (value != null) Approver3 = ListsResource.Instance.ApproverCollection.FirstOrDefault((o) => { return o.StfId == Approve3.Step1Staff; });
            }
        }

        protected EIA.PM.Model.TEngineer _Approver1;
        /// <summary>
        /// 一审人
        /// </summary>
        public EIA.PM.Model.TEngineer Approver1
        {
            get { return _Approver1; }
            set
            {
                _Approver1 = value;
                NotifyPropertyChanged(m => m.Approver1);
            }
        }

        protected EIA.PM.Model.TApprover _Approver2;
        /// <summary>
        /// 二审人
        /// </summary>
        public EIA.PM.Model.TApprover Approver2
        {
            get { return _Approver2; }
            set
            {
                _Approver2 = value;
                NotifyPropertyChanged(m => m.Approver2);
            }
        }

        protected EIA.PM.Model.TApprover _Approver3;
        /// <summary>
        /// 三审人
        /// </summary>
        public EIA.PM.Model.TApprover Approver3
        {
            get { return _Approver3; }
            set
            {
                _Approver3 = value;
                NotifyPropertyChanged(m => m.Approver3);
            }
        }

        #endregion

        #region command

        #region UpdateApprover1Command

        private SimpleMvvmToolkit.DelegateCommand _UpdateApprover1Command = null;
        public SimpleMvvmToolkit.DelegateCommand UpdateApprover1Command
        {
            get
            {
                if (_UpdateApprover1Command == null)
                {
                    _UpdateApprover1Command = new SimpleMvvmToolkit.DelegateCommand(UpdateApprover1, CanExcuteUpdateApprover1);
                } return _UpdateApprover1Command;
            }
            set { _UpdateApprover1Command = value; NotifyPropertyChanged(m => m.UpdateApprover1Command); }
        }

        public void UpdateApprover1()
        {
            EIA.PM.BLL.ClientProxy.LProjectApproveProxy proxy = new EIA.PM.BLL.ClientProxy.LProjectApproveProxy();
            if (Approve1.AppId <= 0)
            {
                Approve1.Step1Staff = Approver1.StfId;
                Approve1.Step3Staff = Approver1.StfId;
                Approve1.Step2Staff = ProjectBasic.PrjLeader;
                Approve1.Step4Staff = ProjectBasic.PrjLeader;
                Approve1.StepState = 1;
                Busy();
                proxy.Insert(Approve1, (k) =>
                {
                    Free();
                    Approve1.AppId = k;
                    System.Windows.MessageBox.Show("提交审核人成功！", "提示", System.Windows.MessageBoxButton.OK);
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("提交审核人发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
            else
            {
                Dictionary<string, object> props = new Dictionary<string, object>();
                props[EIA.PM.Model.LProjectApprove.APP_ID] = Approve1.AppId;
                props[EIA.PM.Model.LProjectApprove.STEP1_STAFF] = Approver1.StfId;
                Busy();
                proxy.UpdateChanged(props, (r) =>
                {
                    Free();
                    Approve1.Step1Staff = Approver1.StfId;
                    Approve1.Step3Staff = Approver1.StfId;
                    System.Windows.MessageBox.Show("提交审核人成功！", "提示", System.Windows.MessageBoxButton.OK);
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("提交审核人发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
        }

        public bool CanExcuteUpdateApprover1()
        {
            return true;
        }

        #endregion

        #region UpdateApprover2Command

        private SimpleMvvmToolkit.DelegateCommand _UpdateApprover2Command = null;
        public SimpleMvvmToolkit.DelegateCommand UpdateApprover2Command
        {
            get
            {
                if (_UpdateApprover2Command == null)
                {
                    _UpdateApprover2Command = new SimpleMvvmToolkit.DelegateCommand(UpdateApprover2, CanExcuteUpdateApprover2);
                } return _UpdateApprover2Command;
            }
            set { _UpdateApprover2Command = value; NotifyPropertyChanged(m => m.UpdateApprover2Command); }
        }

        public void UpdateApprover2()
        {
            EIA.PM.BLL.ClientProxy.LProjectApproveProxy proxy = new EIA.PM.BLL.ClientProxy.LProjectApproveProxy();
            if (Approve2.AppId <= 0)
            {
                Approve2.Step1Staff = Approver2.StfId;
                Approve2.Step3Staff = Approver2.StfId;
                Approve2.Step2Staff = ProjectBasic.PrjLeader;
                Approve2.Step4Staff = ProjectBasic.PrjLeader;
                Approve2.StepState = 1;
                Busy();
                proxy.Insert(Approve2, (k) =>
                {
                    Free();
                    Approve2.AppId = k;
                    System.Windows.MessageBox.Show("提交审核人成功！", "提示", System.Windows.MessageBoxButton.OK);
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("提交审核人发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
            else
            {
                Dictionary<string, object> props = new Dictionary<string, object>();
                props[EIA.PM.Model.LProjectApprove.APP_ID] = Approve2.AppId;
                props[EIA.PM.Model.LProjectApprove.STEP1_STAFF] = Approver2.StfId;
                Busy();
                proxy.UpdateChanged(props, (r) =>
                {
                    Free();
                    Approve2.Step1Staff = Approver2.StfId;
                    Approve2.Step3Staff = Approver2.StfId;
                    System.Windows.MessageBox.Show("提交审核人成功！", "提示", System.Windows.MessageBoxButton.OK);
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("提交审核人发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }

        }

        public bool CanExcuteUpdateApprover2()
        {
            return true;
        }

        #endregion

        #region UpdateApprover3Command

        private SimpleMvvmToolkit.DelegateCommand _UpdateApprover3Command = null;
        public SimpleMvvmToolkit.DelegateCommand UpdateApprover3Command
        {
            get
            {
                if (_UpdateApprover3Command == null)
                {
                    _UpdateApprover3Command = new SimpleMvvmToolkit.DelegateCommand(UpdateApprover3, CanExcuteUpdateApprover3);
                } return _UpdateApprover3Command;
            }
            set { _UpdateApprover3Command = value; NotifyPropertyChanged(m => m.UpdateApprover3Command); }
        }

        public void UpdateApprover3()
        {
            EIA.PM.BLL.ClientProxy.LProjectApproveProxy proxy = new EIA.PM.BLL.ClientProxy.LProjectApproveProxy();
            if (Approve3.AppId <= 0)
            {
                Approve3.Step1Staff = Approver3.StfId;
                Approve3.Step3Staff = Approver3.StfId;
                Approve3.Step2Staff = ProjectBasic.PrjLeader;
                Approve3.Step4Staff = ProjectBasic.PrjLeader;
                Approve3.StepState = 1;
                Busy();
                proxy.Insert(Approve3, (k) =>
                {
                    Free();
                    Approve3.AppId = k;
                    System.Windows.MessageBox.Show("提交审核人成功！", "提示", System.Windows.MessageBoxButton.OK);
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("提交审核人发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
            else
            {
                Dictionary<string, object> props = new Dictionary<string, object>();
                props[EIA.PM.Model.LProjectApprove.APP_ID] = Approve3.AppId;
                props[EIA.PM.Model.LProjectApprove.STEP1_STAFF] = Approver3.StfId;
                Busy();
                proxy.UpdateChanged(props, (r) =>
                {
                    Free();
                    Approve3.Step1Staff = Approver3.StfId;
                    Approve3.Step3Staff = Approver3.StfId;
                    System.Windows.MessageBox.Show("提交审核人成功！", "提示", System.Windows.MessageBoxButton.OK);
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("提交审核人发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
        }

        public bool CanExcuteUpdateApprover3()
        {
            return true;
        }

        #endregion

        #region SaveApprove1Command

        private SimpleMvvmToolkit.DelegateCommand _SaveApprove1Command = null;
        public SimpleMvvmToolkit.DelegateCommand SaveApprove1Command
        {
            get
            {
                if (_SaveApprove1Command == null)
                {
                    _SaveApprove1Command = new SimpleMvvmToolkit.DelegateCommand(SaveApprove1, CanExcuteSaveApprove1);
                } return _SaveApprove1Command;
            }
            set { _SaveApprove1Command = value; NotifyPropertyChanged(m => m.SaveApprove1Command); }
        }

        public void SaveApprove1()
        {
            EIA.PM.BLL.ClientProxy.LProjectApproveProxy proxy = new EIA.PM.BLL.ClientProxy.LProjectApproveProxy();

            Busy();
            proxy.UpdateChanged(Approve1.GetPropertyChange(true), (r) =>
            {
                Free();
                if (r)
                {
                    System.Windows.MessageBox.Show("提交成功！", "提示", System.Windows.MessageBoxButton.OK);
                    Approve1.ClearPropertyChange();
                }
                else
                {
                    System.Windows.MessageBox.Show("提交失败！", "提示", System.Windows.MessageBoxButton.OK);
                }
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("提交发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteSaveApprove1()
        {
            return true;
        }

        #endregion

        #region SaveApprove2Command

        private SimpleMvvmToolkit.DelegateCommand _SaveApprove2Command = null;
        public SimpleMvvmToolkit.DelegateCommand SaveApprove2Command
        {
            get
            {
                if (_SaveApprove2Command == null)
                {
                    _SaveApprove2Command = new SimpleMvvmToolkit.DelegateCommand(SaveApprove2, CanExcuteSaveApprove2);
                } return _SaveApprove2Command;
            }
            set { _SaveApprove2Command = value; NotifyPropertyChanged(m => m.SaveApprove2Command); }
        }

        public void SaveApprove2()
        {
            EIA.PM.BLL.ClientProxy.LProjectApproveProxy proxy = new EIA.PM.BLL.ClientProxy.LProjectApproveProxy();

            Busy();
            proxy.UpdateChanged(Approve2.GetPropertyChange(true), (r) =>
            {
                Free();
                if (r)
                {
                    System.Windows.MessageBox.Show("提交成功！", "提示", System.Windows.MessageBoxButton.OK);
                    Approve2.ClearPropertyChange();
                }
                else
                {
                    System.Windows.MessageBox.Show("提交失败！", "提示", System.Windows.MessageBoxButton.OK);
                }
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("提交发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });

        }

        public bool CanExcuteSaveApprove2()
        {
            return true;
        }

        #endregion

        #region SaveApprove3Command

        private SimpleMvvmToolkit.DelegateCommand _SaveApprove3Command = null;
        public SimpleMvvmToolkit.DelegateCommand SaveApprove3Command
        {
            get
            {
                if (_SaveApprove3Command == null)
                {
                    _SaveApprove3Command = new SimpleMvvmToolkit.DelegateCommand(SaveApprove3, CanExcuteSaveApprove3);
                } return _SaveApprove3Command;
            }
            set { _SaveApprove3Command = value; NotifyPropertyChanged(m => m.SaveApprove3Command); }
        }

        public void SaveApprove3()
        {
            EIA.PM.BLL.ClientProxy.LProjectApproveProxy proxy = new EIA.PM.BLL.ClientProxy.LProjectApproveProxy();

            Busy();
            proxy.UpdateChanged(Approve3.GetPropertyChange(true), (r) =>
            {
                Free();
                if (r)
                {
                    System.Windows.MessageBox.Show("提交成功！", "提示", System.Windows.MessageBoxButton.OK);
                    Approve3.ClearPropertyChange();
                }
                else
                {
                    System.Windows.MessageBox.Show("提交失败！", "提示", System.Windows.MessageBoxButton.OK);
                }
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("提交发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });

        }

        public bool CanExcuteSaveApprove3()
        {
            return true;
        }

        #endregion

        #endregion


        public void OnLoad()
        {
            this.SendMessage<object, EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, PM.Model.TProjectBasic>("", null, (p) =>
            {
                ProjectBasic = p;
            }));
            EIA.PM.BLL.ClientProxy.LProjectApproveProxy proxy = new EIA.PM.BLL.ClientProxy.LProjectApproveProxy();
            Dictionary<string, object> con = new Dictionary<string, object>();
            con[EIA.PM.Model.LProjectApprove.PRJ_ID] = ProjectBasic.PrjId;
            Busy();
            proxy.List(con, 3, 1, (lst) =>
            {
                Free();
                if (lst != null && lst.Count != 0)
                {
                    Approve1 = lst.FirstOrDefault((o) => { return o.Progress == 6; });
                    Approve2 = lst.FirstOrDefault((o) => { return o.Progress == 7; });
                    Approve3 = lst.FirstOrDefault((o) => { return o.Progress == 8; });
                }
                if (ProjectBasic.Progress <= 6)
                {
                    if (Approve1 == null) Approve1 = new PM.Model.LProjectApprove() { PrjId = ProjectBasic.PrjId, Progress = 6, StepState = 0 };
                }
                else if (ProjectBasic.Progress <= 7)
                {
                    if (Approve2 == null) Approve2 = new PM.Model.LProjectApprove() { PrjId = ProjectBasic.PrjId, Progress = 7, StepState = 0 };
                }
                else if (ProjectBasic.Progress >= 8)
                {
                    if (Approve3 == null) Approve3 = new PM.Model.LProjectApprove() { PrjId = ProjectBasic.PrjId, Progress = 8, StepState = 0 };
                }

            }, (ex) =>
            {
                Free();
            });
        }
    }
}